import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'
import { map } from 'rxjs'
import { Observable } from 'rxjs'


interface Response<T> {
  data: T
}

/**
 * 成功拦截器
 */
@Injectable()
export class TransformInterceptor<T> implements NestInterceptor<T, Response<T>>{
  intercept(context: ExecutionContext, next: CallHandler): Observable<Response<T>> {
    return next.handle().pipe(map(data => {
      if (data?.stream) return data
      return { data, status: 0, extra: {}, message: "success", success: true }
    }))
  }
}